package com.ovhoo.vdm.vdm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Stack;


import android.os.Handler;
import android.os.Message;

/**
 * This thread should be the only thread used by the application to download from VDM
 * @author mzalim
 *
 */
public class VDMControler extends Thread {
	
	private static VDMControler uniqueInstance = null; //Reference to the unique instance of this class
	
	//Requests
	private Stack<VDMDownloadRequest> requests;
	
	
	//VDM Loader Quotes
	ArrayList<VDMQuote>  quotes =  null;
    
	
	//Manage thread
	private boolean isRunning;	//Indicate if the Thred is running
	
			
	//Constructors
	private VDMControler(){
		super();
		
		this.isRunning = false;
		this.requests = new Stack<VDMDownloadRequest>() ;
	}
	
	/**
	 * returns the unique instance of this class
	 * @return
	 */
	public static VDMControler getInstance(){
		if (VDMControler.uniqueInstance == null){
			VDMControler.uniqueInstance = new  VDMControler();
		}
		
		return VDMControler.uniqueInstance;
	}
	
	//Private methodes
	/**
	 * End an executed request
	 * @param result : true if the request was executed correctly
	 */
	private void endRequest(VDMDownloadRequest req, boolean result){
		req.achieve(quotes);
	}
	
	
	//Public Methodes
	public void start(){
		super.start();
		this.isRunning = true;
	}
	
	public void run(){
		super.run();
		
		try {
			while( this.isRunning ){
				
				//get a request if there is one
				if (!this.requests.empty()){
					VDMDownloadRequest _res  = this.requests.pop();
					switch (_res.getType()){
					
					case LOAD_LATEST_QUOTES :
						quotes =  VDMLoader.getLastPosts(_res.getLangage());
						this.endRequest(_res, true);
						break;
					case LOAD_RANDOM_QUOTES :
						quotes =  VDMLoader.getRandomPosts(_res.getLangage());
						this.endRequest(_res, true);
						break;
					case LOAD_TOP_QUOTES :
						quotes =  VDMLoader.getTopPosts(_res.getLangage());
						this.endRequest(_res, true);
						break;
					case LOAD_FLOP_QUOTES :
						quotes =  VDMLoader.getFlopPosts(_res.getLangage());
						this.endRequest(_res, true);
						break;
					case LOAD_QUOTE_COMMENTS_BY_ID :
						quotes =  VDMLoader.getuoteByID(_res.getLangage(), _res.getParam());
						this.endRequest(_res, true);
						break;		
						
					//Areteer le thread
					case IDLE :
					default :
						this.sleep(2000);
					}
				}else{
					this.sleep(2000);
				}			
				
				//Dormir un peu pour ne pas chaufer notre petit pross
				this.sleep(500);
			}
			
		} catch (InterruptedException e) {
				e.printStackTrace();
		}
	}
	
	
	/**
	 * Request someting to the VDM controler
	 * @param type : the request type
	 * @param param : the request parameter
	 * @param msg message to send when the request was fulfiled
	 */
	public void makeARequest(VDMDownloadRequest.requestType type, int param, Message msg, String langage){
		VDMDownloadRequest _req = new VDMDownloadRequest(type, param, msg, langage);
		this.requests.push(_req);
		
		if (!this.isRunning){
			this.start();
		}
	}
	
	
	
	/**
	 * Return the quotes
	 * @return the result of the lates quotes
	 */
	public ArrayList<VDMQuote> getQuotes(){
		return this.quotes;
	}

}
